//
//  LXConcernUserInfoCell.m
//  cat
//
//  Created by imera on 16/3/29.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXConcernUserInfoCell.h"
#import "UIImageView+WebCache.h"
#import "UIImage+Lx.h"

#define LXUserInfoCellViewMargin 10

@interface LXConcernUserInfoCell()
/** 用户头像 */
@property (nonatomic, weak) UIImageView *userPhotoImageView;
/** 用户姓名 */
@property (nonatomic, weak) UILabel *userNameLabel;
/** 用户等级 */
@property (nonatomic, weak) UIImageView *userLevelImageView;
/** 用户性别 */
@property (nonatomic, weak) UIImageView *userSexImageView;
/** 用户简介 */
@property (nonatomic, weak) UILabel *userInfoLabel;
/** 关注按钮 */
@property (nonatomic, weak) UIButton *concernBtn;
@end

@implementation LXConcernUserInfoCell

- (void)setTitle:(NSString *)title
{
    if (title != nil) {
        _title = title;
        [self layoutSubviews];
    }
}

- (void)setUser:(LXUser *)user
{
    if (user != nil) {
        _user = user;
    }
    [self layoutSubviews];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self initSubViews];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

#pragma mark - 初始化子控件
- (void)initSubViews
{
    UIImageView *userPhotoImageView = [[UIImageView alloc]init];
    self.userPhotoImageView = userPhotoImageView;
    [self addSubview:userPhotoImageView];
    
    UILabel *userNameLabel = [[UILabel alloc]init];
    self.userNameLabel = userNameLabel;
    [self addSubview:userNameLabel];
    
    UIImageView *userLevelImageView = [[UIImageView alloc]init];
    self.userLevelImageView = userLevelImageView;
    [self addSubview:userLevelImageView];
    
    UIImageView *userSexImageView = [[UIImageView alloc]init];
    self.userSexImageView = userSexImageView;
    [self addSubview:userSexImageView];
    
    UILabel *userInfoLabel = [[UILabel alloc]init];
    self.userInfoLabel = userInfoLabel;
    [self addSubview:userInfoLabel];
    
    UIButton *concernBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.concernBtn = concernBtn;
    [self addSubview:concernBtn];
}

#pragma mark - 设置子控件数据
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    /** 用户头像 */
    UIImageView *userPhotoImageView = self.userPhotoImageView;
    [userPhotoImageView sd_setImageWithURL:[NSURL URLWithString:self.user.headpic]];
    
    CGFloat userPhotoImageViewX = LXUserInfoCellViewMargin;
    CGFloat userPhotoImageViewY = LXUserInfoCellViewMargin * 0.5;
    CGFloat userPhotoImageViewH = (self.frame.size.height - userPhotoImageViewY * 2);
    CGFloat userPhotoImageViewW = userPhotoImageViewH;
    userPhotoImageView.frame = CGRectMake(userPhotoImageViewX, userPhotoImageViewY, userPhotoImageViewW, userPhotoImageViewH);
    
    //设置圆角
    userPhotoImageView.layer.masksToBounds = YES;
    userPhotoImageView.layer.cornerRadius = userPhotoImageViewW * 0.5;
    userPhotoImageView.layer.borderWidth = 2;
    userPhotoImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    /** 用户姓名 */
    UILabel *userNameLabel = self.userNameLabel;
    userNameLabel.text = [self.user.uname isEqualToString:@""] ? self.user.email : self.user.uname;
    userNameLabel.font = [UIFont boldSystemFontOfSize:15];
    
    CGSize userNameLabelSize = [userNameLabel.text sizeWithAttributes:@{NSFontAttributeName:userNameLabel.font}];
    CGFloat userNameLabelX = CGRectGetMaxX(userPhotoImageView.frame) + LXUserInfoCellViewMargin;
    CGFloat userNameLabelY = userPhotoImageViewY;
    CGFloat userNameLabelW = userNameLabelSize.width;
    CGFloat userNameLabelH = userNameLabelSize.height;
    userNameLabel.frame = CGRectMake(userNameLabelX, userNameLabelY, userNameLabelW, userNameLabelH);
    
    /** 用户等级 */
    UIImageView *userLevelImageView = self.userLevelImageView;
    NSString *level = [NSString stringWithFormat:@"vip_icon%ld",self.user.ulevel];
    userLevelImageView.image = [UIImage imageNamed:level];
    
    CGFloat userLevelImageViewX = CGRectGetMaxX(userNameLabel.frame) + 5;
    CGFloat userLevelImageViewY = userNameLabelY + 2;
    CGFloat userLevelImageViewW = 15;
    CGFloat userLevelImageViewH = 15;
    userLevelImageView.frame = CGRectMake(userLevelImageViewX, userLevelImageViewY, userLevelImageViewW, userLevelImageViewH);
    
    /** 用户性别 */
    UIImageView *userSexImageView = self.userSexImageView;
    if (self.user.sex == 0) {
        userSexImageView.image = [UIImage imageNamed:@"man"];
    }else{
        userSexImageView.image = [UIImage imageNamed:@"women"];
    }
    
    
    CGFloat userSexImageViewX = CGRectGetMaxX(userLevelImageView.frame) + 5;
    CGFloat userSexImageViewY = userNameLabelY + 1;
    CGFloat userSexImageViewW = 18;
    CGFloat userSexImageViewH = 18;
    userSexImageView.frame = CGRectMake(userSexImageViewX, userSexImageViewY, userSexImageViewW, userSexImageViewH);
    
    /** 用户简介 */
    UILabel *userInfoLabel = self.userInfoLabel;
    userInfoLabel.text = self.user.info;
    userInfoLabel.font = [UIFont systemFontOfSize:12];
    userInfoLabel.numberOfLines = 2;
    
    CGSize userInfoLabelSize = [userInfoLabel.text sizeWithAttributes:@{NSFontAttributeName:userInfoLabel.font}];
    CGFloat userInfoLabelX = userNameLabelX;
    CGFloat userInfoLabelY = CGRectGetMaxY(userNameLabel.frame) + LXUserInfoCellViewMargin;
    CGFloat userInfoLabelW = self.frame.size.width - userNameLabelX - 70;
    CGFloat userInfoLabelH = userInfoLabelSize.height * 2;
    userInfoLabel.frame = CGRectMake(userInfoLabelX, userInfoLabelY, userInfoLabelW, userInfoLabelH);
    
    UIButton *concernBtn = self.concernBtn;
    concernBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [concernBtn setTitle:self.title forState:UIControlStateNormal];
    [concernBtn setBackgroundImage:[UIImage resizeWithName:@"button-blue"] forState:UIControlStateNormal];
    [concernBtn addTarget:self action:@selector(concern:) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat concernBtnW = 50;
    CGFloat concernBtnH = 20;
    CGFloat concernBtnX = self.frame.size.width - concernBtnW - 10;
    CGFloat concernBtnY = (self.frame.size.height - concernBtnH) / 2;
    concernBtn.frame = CGRectMake(concernBtnX, concernBtnY, concernBtnW, concernBtnH);
}

- (void)concern:(UIButton *)btn
{
    if ([self.MyDelegate respondsToSelector:@selector(concernUserInfoCell:title:)]) {
        BOOL sucess = [self.MyDelegate concernUserInfoCell:self title:btn.titleLabel.text];
        NSString *title = [btn.titleLabel.text isEqualToString:@"关注"] ? @"已关注" : @"关注";
        if (sucess) {
            [btn setTitle:title forState:UIControlStateNormal];
        }
    }
}

@end
